package com.bjb.vr.manager.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjb.vr.common.mapper.DevicesStatisticsMapper;
import com.bjb.vr.common.utils.DateTimeUtil;
import com.bjb.vr.common.vo.CourseCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @version 1.0.0
 * @author: HuJingBo
 * @time: 2022/5/9 11:25
 */
@Service
public class DataStatisticsService {

    @Autowired
    private DevicesStatisticsMapper devicesStatisticsMapper;

    /**
     * 获取设备统计数据
     *
     * @return
     */
    public JSONArray devicesCount() {
        HashMap<String, Integer> countDevicesStatus = devicesStatisticsMapper.devicesStatusCount();
        HashMap<String, BigDecimal> countDevicesUseTimeOfDay = devicesStatisticsMapper.devicesUseTimeCount(DateTimeUtil.nowStrTime(DateTimeUtil.DATE_FORMAT_DAY), DateTimeUtil.nowStrTime(DateTimeUtil.DATE_FORMAT_DAY));
        HashMap<String, BigDecimal> countDevicesUseTimeOfWeek = devicesStatisticsMapper.devicesUseTimeCount(DateTimeUtil.firstDayOfWeek(), DateTimeUtil.lastDayOfWeek());
        HashMap<String, BigDecimal> countDevicesUseTimeOfMonth = devicesStatisticsMapper.devicesUseTimeCount(DateTimeUtil.firstDayOfMonth(), DateTimeUtil.lastDayOfMonth());

        // 设备状态统计
        HashMap statusMap = new HashMap();
        JSONArray statusArray = new JSONArray();
        for (Map.Entry<String, Integer> entry : countDevicesStatus.entrySet()) {
            JSONObject object = new JSONObject();
            object.put("name", entry.getKey());
            object.put("value", entry.getValue());
            statusArray.add(object);
        }
        statusMap.put("text", "设备状态统计");
        statusMap.put("status", statusArray);

        // 设备使用时长统计
        HashMap useTimeMap = new HashMap();
        JSONArray useTimeArray = new JSONArray();

        JSONObject object1 = new JSONObject();
        object1.put("name", "day");
        object1.put("value", countDevicesUseTimeOfDay.get("avgUseTime"));
        useTimeArray.add(object1);

        JSONObject object2 = new JSONObject();
        object2.put("name", "week");
        object2.put("value", countDevicesUseTimeOfWeek.get("avgUseTime"));
        useTimeArray.add(object2);

        JSONObject object3 = new JSONObject();
        object3.put("name", "month");
        object3.put("value", countDevicesUseTimeOfMonth.get("avgUseTime"));
        useTimeArray.add(object3);

        useTimeMap.put("text", "设备使用时长统计");
        useTimeMap.put("useTime", useTimeArray);

        // 信息汇总
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(statusMap);
        jsonArray.add(useTimeMap);
        return jsonArray;
    }

    /**
     * 获取资源统计数据
     *
     * @return
     */
    public JSONArray resourcesCount() {
        HashMap<String, Integer> countByLevel = devicesStatisticsMapper.resourcesCountByLevel();
        HashMap<String, Integer> countBySubject = devicesStatisticsMapper.resourcesCountBySubject();
        HashMap<String, Integer> countByType = devicesStatisticsMapper.resourcesCountByType();

        // 课程年级分类
        HashMap levelMap = new HashMap();
        JSONArray levelArray = new JSONArray();
        if (Objects.nonNull(countByLevel)) {
            for (Map.Entry<String, Integer> entry : countByLevel.entrySet()) {
                JSONObject object = new JSONObject();
                object.put("name", entry.getKey());
                object.put("value", entry.getValue());
                levelArray.add(object);
            }
        }
        levelMap.put("text", "课程年级分类");
        levelMap.put("level", levelArray);

        // 课程学科分类
        HashMap subjectMap = new HashMap();
        JSONArray subjectArray = new JSONArray();
        if (Objects.nonNull(countBySubject)) {
            for (Map.Entry<String, Integer> entry : countBySubject.entrySet()) {
                JSONObject object = new JSONObject();
                object.put("name", entry.getKey());
                object.put("value", entry.getValue());
                subjectArray.add(object);
            }
        }
        subjectMap.put("text", "课程学科分类");
        subjectMap.put("subject", subjectArray);

        // 课程类型分类
        HashMap typeMap = new HashMap();
        JSONArray typeArray = new JSONArray();
        if (Objects.nonNull(countByType)) {
            for (Map.Entry<String, Integer> entry : countByType.entrySet()) {
                JSONObject object = new JSONObject();
                object.put("name", entry.getKey());
                object.put("value", entry.getValue());
                typeArray.add(object);
            }
        }
        typeMap.put("text", "课程类型分类");
        typeMap.put("type", typeArray);

        // 信息汇总
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(levelMap);
        jsonArray.add(subjectMap);
        jsonArray.add(typeMap);
        return jsonArray;
    }

    /**
     * 获取课程热度统计数据
     *
     * @return
     */
    public JSONArray resourcesUseCount() {
        List<CourseCountVo> courseCountVos = devicesStatisticsMapper.resourcesUseCount();
        // 课程热度统计
        HashMap hotMap = new HashMap();
        JSONArray hotArray = new JSONArray();
        courseCountVos.stream().forEach(c -> {
            JSONObject object = new JSONObject();
            object.put("name", c.getName());
            object.put("value", c.getUseCount());
            hotArray.add(object);
        });
        hotMap.put("text", "课程热度统计");
        hotMap.put("useCount", hotArray);

        // 信息汇总
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(hotMap);
        return jsonArray;
    }
}
